import dayjs, { type Dayjs, type ManipulateType, type OpUnitType } from 'dayjs';
import 'dayjs/locale/zh-cn';
import timezone from 'dayjs/plugin/timezone';
import utc from 'dayjs/plugin/utc';
import weekday from 'dayjs/plugin/weekday'; // ES 2015

dayjs.extend(utc);
dayjs.extend(timezone);
dayjs.locale('zh-cn');
dayjs.extend(weekday);

export const formatDate = (
  date: number | string | Date | Dayjs = dayjs(),
  formatString = 'YYYY-MM-DD'
) => dayjs(date).format(formatString);

export const formatUtcDate = (
  date: number | string | Date | Dayjs = dayjs(),
  formatString = 'YYYY-MM-DD'
) => dayjs(date).utc().format(formatString);

export const today = formatDate();

export const getStartOrEndTimeStamp = (
  dateType: OpUnitType,
  date = dayjs()
) => ({
  start: dayjs(date).startOf(dateType).valueOf(),
  end: dayjs(date).endOf(dateType).valueOf()
});

export const getStartOrEndDate = (dateType: OpUnitType, date = dayjs()) => ({
  start: dayjs(date).startOf(dateType),
  end: dayjs(date).endOf(dateType)
});

export const addDate = (
  date: number | string | Date | Dayjs,
  num: number,
  type: ManipulateType
) => dayjs(date).add(num, type);

/** 获取今年日期 开始日期是明天 结束日期是今年最后一天 */
export const getCurrentYearZone = () => {
  return {
    start: dayjs().add(1, 'day').startOf('day').valueOf(),
    end: dayjs().endOf('year').valueOf()
  };
};

/** 格式化日期使用 */
export const formatTimestamp = (date: string | number) => dayjs(date).valueOf();

/** 判断是否相等 */
export const isEqualDate = (
  date: number | string | Date | Dayjs,
  date1: number | string | Date | Dayjs,
  type: OpUnitType
) => {
  return dayjs(date).isSame(dayjs(date1), type);
};
